package com.core.controller;

import com.core.annotation.OperationLog;
import com.core.dto.RoleInfoDto;
import com.core.resp.AjaxResponse;
import com.core.resp.PageResult;
import com.core.service.RolesService;
import com.core.vo.RolePermissionVo;
import com.core.vo.RoleVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : HUANG
 * @create : 2025/6/1 8:34
 * @function :
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    private static final Logger log = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private RolesService rolesService;
    @PreAuthorize("@ss.hasAuthority('role:list')")
    @PostMapping("/get/page/{pageSize}/{pageNum}")
    @OperationLog(type = "list", module = "role", description = "分页获取角色列表")
    public AjaxResponse getPage(@PathVariable Long pageNum, @PathVariable Long pageSize, @RequestBody RoleInfoDto roleInfoDto) {
        log.error(roleInfoDto.toString());
        try {
            PageResult<RoleInfoDto> result = rolesService.selectPage(pageSize, pageNum, roleInfoDto);
            return AjaxResponse.success(200, "查询成功", result);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResponse.error(405, "查询错误");
        }
    }

    @PreAuthorize("@ss.hasAuthority('role:create')")
    @PostMapping("/add")
    @OperationLog(type = "create", module = "role", description = "添加角色: #role.description")
    public AjaxResponse addRole(@RequestBody RoleVo roleVo) {
        try {
            return rolesService.addRole(roleVo);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResponse.error(405, "服务器错误添加失败");
        }
    }

    @PreAuthorize("@ss.hasAuthority('role:delete')")
    @OperationLog(type = "delete", module = "role", description = "删除角色: #role.description")
    @PostMapping("/batchDelete")
    public AjaxResponse deleteRole(@RequestBody List<Long> ids) {
        log.info(ids.toString());
        try {
            return rolesService.deleteRoles(ids);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResponse.error(405, "删除失败");
        }
    }

    @PreAuthorize("@ss.hasAuthority('role:update')")
    @PutMapping("/update")
    @OperationLog(type = "update", module = "role", description = "更新角色, 角色ID: #role.id, 描述: #role.description")
    public AjaxResponse update(@RequestBody RoleInfoDto roleInfoDto) {
        try {
            rolesService.updateRole(roleInfoDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResponse.error(405, "更新失败");
        }
        return AjaxResponse.success(200, "更新成功", null);
    }

    @PreAuthorize("@ss.hasAuthority('role_permission:assign')")
    @PutMapping("permissions/update")
    @OperationLog(type = "assign", module = "role_permission", description = "更新角色权限, 角色ID: #rolePermissionsVo.roleId, 权限IDs: #rolePermissionsVo.permissionIds")
    public AjaxResponse updatePermission(@RequestBody RolePermissionVo rolePermissionVo) {
        try {
            rolesService.updatePermission(rolePermissionVo);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResponse.error(405, "修改失败");
        }
        return AjaxResponse.success(200, "修改成功", null);
    }
}
